﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace K054539
{
	public class CodecInt : CodecInterface.ICodec
	{
		private CodecProperties properties = new CodecProperties();

		public string this[string property]
		{
			get
			{
				switch (property.ToLower())
				{
					case @"bitspersample":
						return "16";
					case @"channels":
						return "1";
					case @"loopenabled":
						return properties.LoopEnabled.ToString();
					case @"looplength":
						return properties.LoopLength.ToString();
					case @"loopstart":
						return properties.LoopStart.ToString();
					case @"samplerate":
						return "24000";
				}
				return "";
			}

			set
			{
				switch (property.ToLower())
				{
					case @"dpcmtable":
						switch (int.Parse(value))
						{
							case 0:
								properties.DPCMTable = DPCMTableDefault.Shifts();
								break;
							case 1:
								properties.DPCMTable = DPCMTableDefault.Squares();
								break;
						}
						break;
					case @"swapnybble":
						properties.SwapNybble = Convert.ToBoolean(value);
						break;
				}
			}
		}

		public bool Decode(Stream source, Stream target)
		{
			properties.Input = source;
			properties.Output = target;

			Decoder decoder = new Decoder(properties);
			BinaryWriter writer = new BinaryWriter(target);
			short[] samples = decoder.Decode();

			if (samples != null)
			{
				int count = samples.Length;
				for (int i = 0; i < count; i++)
				{
					writer.Write(samples[i]);
				}
				writer.Flush();
			}
			else
			{
				return false;
			}
			return true;
		}

		public bool Encode(Stream source, Stream target)
		{
			return false;
		}
	}
}
